package masterjava.common.client.gxt.highstock;

import com.google.gwt.core.client.JavaScriptObject;
import masterjava.common.client.gxt.base.util.JsonObject;
import masterjava.common.client.gxt.base.util.ScriptUtil;
import masterjava.common.shared.Presentable;
import masterjava.common.shared.util.SharedUtil;

/**
 * User: gkislin
 * Date: 7/21/11
 */
public class HighStockSeries<I> extends JsonObject {
    private final JavaScriptObject jsonObject;

    public HighStockSeries(String name, Iterable<I> data, final Presentable<I> presenter) {
        setProperty("name", name);
        jsonObject = ScriptUtil.evalJsonObject("[" + SharedUtil.collectionToDelimitedString(data, ",", "[", "]",
                new Presentable<I>() {
                    @Override
                    public CharSequence present(I object) {
                        return presenter.present(object);
                    }
                }) + "]");
    }

    public void setType(HighStockSeriesType type) {
        removeProperty("type");
        removeProperty("step");
        removeProperty("lineWidth");
        setMarker(0);
        switch (type) {
            case line:
                break;
            case step:
                setProperty("step", true);
                break;
            case points:
                setProperty("lineWidth", 0);
                setMarker(2);
                break;
            default:
                setProperty("type", type.name());
        }
    }

    public void setMarker(int radius) {
        if (radius == 0) {
            setProperty("marker", null);
        } else {
            setProperty("marker/enabled", true);
            setProperty("marker/radius", radius);
        }
    }

    public void setShadow(boolean isShadow) {
        setProperty("shadow", isShadow);
    }

    public void setDataBeforeDraw() {
        setProperty("data", jsonObject);
    }
}
